#include "TextureLoaderNotifier.h"
#include "TextureFrame.h"

namespace liba {
namespace lib3d {
namespace material {

TextureLoadingManager* TextureLoadingManager::instance;

TextureLoadingManager::TextureLoadingManager()
{
}

TextureLoadingManager* TextureLoadingManager::GetInstance()
{
	if(instance)
		return instance;

	return instance = new TextureLoadingManager();
}

void TextureLoadingManager::LifeCycle()
{
	std::vector <TextureLoadingNotifier*>::iterator it = notifiers.begin();
	while(it != notifiers.end())
	{
		if((*it)->texture->IsReady())
		{
			(*it)->Invoke();
			delete *it;

			notifiers.erase(it);
			it = notifiers.begin();
		}
		else
			it++;
	}
}

void TextureLoadingManager::AddNotifier(TextureFrame *texture, TextureLoadingNotifier::OnLoadedCallback callback)
{
	notifiers.push_back(new TextureLoadingNotifier(texture, callback));
}




void TextureLoadingNotifier::AddTexture(TextureFrame *texture, OnLoadedCallback callback)
{
	TextureLoadingManager::GetInstance()->AddNotifier(texture, callback);
}

void TextureLoadingNotifier::Invoke()
{
	callback(texture);
}

} // namespace material
} // namespace lib3d
} // namespace liba